package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.VerticalDividerItemDecoration;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Problem;
import com.tim.app.server.entry.SelectInfo;
import com.tim.app.server.entry.Topic;
import com.tim.app.server.entry.User;
import com.tim.app.ui.activity.selected.QuestionAndAnswerDetailActivity;
import com.tim.app.ui.activity.selected.TopicDetailActivity;
import com.tim.app.ui.activity.selected.TopicSquareActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 内容列表的适配器
 */
public class SelectedInfoItemAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, SelectInfo> {

    private Context mContext;

    private static final int TYPE_ARTICLE = 1;//文章
    private static final int TYPE_ANSWER = 2;//回答
    private static final int TYPE_PROBLEM = 3;//问题
    private static final int TYPE_SUBJECT = 4;//话题

    public SelectedInfoItemAdapter(Context mContext, List<SelectInfo> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        switch (viewType) {
            case TYPE_ARTICLE:
                holder = new ArticleViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_select_article, parent, false));
                break;
            case TYPE_ANSWER:
                holder = new AnswerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_select_answer, parent, false));
                break;
            case TYPE_PROBLEM:
                holder = new ProblemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_select_problem, parent, false));
                break;
            case TYPE_SUBJECT:
                holder = new SubjectViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_select_subject, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        String type = getDataList().get(position).getDiscoverType();
        if (AppKey.TYPE_ARTICLE.equals(type)) {
            return TYPE_ARTICLE;
        } else if (AppKey.TYPE_ANSWER.equals(type)) {
            return TYPE_ANSWER;
        } else if (AppKey.TYPE_PROBLEM.equals(type)) {
            return TYPE_PROBLEM;
        } else if (AppKey.TYPE_TOPIC.equals(type)) {
            return TYPE_SUBJECT;
        } else {
            return TYPE_PROBLEM;
        }
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, SelectInfo data) {
        if (data == null) {
            return;
        }
        if (mHolder instanceof ArticleViewHolder) {
            ArticleViewHolder articleViewHolder = (ArticleViewHolder) mHolder;
            updateArticleViewHolder(articleViewHolder, position, data);
        } else if (mHolder instanceof AnswerViewHolder) {
            AnswerViewHolder answerViewHolder = (AnswerViewHolder) mHolder;
            updateAnswerViewHolder(answerViewHolder, position, data);
        } else if (mHolder instanceof ProblemViewHolder) {
            ProblemViewHolder problemViewHolder = (ProblemViewHolder) mHolder;
            updateProblemViewHolder(problemViewHolder, position, data);
        } else if (mHolder instanceof SubjectViewHolder) {
            SubjectViewHolder subjectViewHolder = (SubjectViewHolder) mHolder;
            updateSubjectViewHolder(subjectViewHolder, position, data);
        }
    }

    /**
     * 更新文章UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateArticleViewHolder(ArticleViewHolder holder, int position, final SelectInfo data) {

        if (null != data.getUserMsg()) {
            if (!TextUtils.isEmpty(data.getUserMsg().getImage())) {
                BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            } else {
                BitmapLoader.ins().loadImage("", holder.rivAvatar);
            }
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            }
            holder.rivAvatar.setTag(data.getUserMsg());
            holder.tvUserName.setTag(data.getUserMsg());
            holder.rivAvatar.setOnClickListener(onClickListener);
            holder.tvUserName.setOnClickListener(onClickListener);
        }

        holder.tvFromSuject.setText(mContext.getString(R.string.article));
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvContent.setText(data.getContent());
        }
        holder.tvPriseNumber.setText(mContext.getString(R.string.selectPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvCommentNumber.setText(mContext.getString(R.string.selectCommentNumber, String.valueOf(data.getCommentCount())));
        if (!TextUtils.isEmpty(data.getImage())) {
            holder.rivCover.setVisibility(View.VISIBLE);
            BitmapLoader.ins().loadImage(data.getImage(), holder.rivCover);
        } else {
            holder.rivCover.setVisibility(View.GONE);
        }
    }

    /**
     * 更新回答UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateAnswerViewHolder(AnswerViewHolder holder, int position, final SelectInfo data) {
        if (null != data.getUserMsg()) {
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            }
            holder.rivAvatar.setTag(data.getUserMsg());
            holder.tvUserName.setTag(data.getUserMsg());
            holder.rivAvatar.setOnClickListener(onClickListener);
            holder.tvUserName.setOnClickListener(onClickListener);
        }
        if (null != data.getImageUrl()) {
            List<String> dataImageUrl = data.getImageUrl();
            holder.llImageContainer.setVisibility(View.VISIBLE);
            if (data.getImageUrl().size() == 1) {
                holder.llMoreImageContainer.setVisibility(View.GONE);
                holder.rivCover.setVisibility(View.VISIBLE);
                BitmapLoader.ins().loadImage(dataImageUrl.get(0), holder.rivCover);
            } else {
                holder.llMoreImageContainer.setVisibility(View.VISIBLE);
                holder.rivCover.setVisibility(View.GONE);
                BitmapLoader.ins().loadImage(dataImageUrl.get(0), holder.rivCoverOne);
                BitmapLoader.ins().loadImage(dataImageUrl.get(1), holder.rivCoverTwo);
                BitmapLoader.ins().loadImage(dataImageUrl.get(2), holder.rivCoverThree);
            }
        } else {
            holder.llImageContainer.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getTopicTitle())) {
            holder.tvFromSuject.setText(mContext.getString(R.string.answer, data.getTopicTitle()));
        } else {
            holder.tvFromSuject.setText(mContext.getString(R.string.answer, ""));
        }
        if (!TextUtils.isEmpty(data.getProblemTitle())) {
            holder.tvTitle.setText(data.getProblemTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvContent.setText(data.getContent());
        }
        holder.tvPriseNumber.setText(mContext.getString(R.string.selectAnswerPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvCommentNumber.setText(mContext.getString(R.string.selectCommentNumber, String.valueOf(data.getCommentCount())));
        holder.tvFromSuject.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(data.getTopicId())) {
                    Topic subject = new Topic();
                    subject.setTopicName(data.getTopicTitle());

                    String topicId = data.getTopicId();
                    subject.setId(topicId);
                    TopicDetailActivity.start(mContext, subject, false);
                }
            }
        });

        holder.tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Problem problem = new Problem();
                problem.setId(String.valueOf(data.getProblemId()));
                QuestionAndAnswerDetailActivity.start(mContext, problem, false);
            }
        });
    }

    /**
     * 更新问题UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateProblemViewHolder(ProblemViewHolder holder, int position, final SelectInfo data) {
        if (null != data.getUserMsg()) {
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            }
            holder.rivAvatar.setTag(data.getUserMsg());
            holder.tvUserName.setTag(data.getUserMsg());
            holder.rivAvatar.setOnClickListener(onClickListener);
            holder.tvUserName.setOnClickListener(onClickListener);
        }

        if (null != data.getImageUrl()) {
            holder.llImageContainer.setVisibility(View.VISIBLE);
            if (data.getImageUrl().size() == 1) {
                holder.llMoreImageContainer.setVisibility(View.GONE);
                holder.rivCover.setVisibility(View.VISIBLE);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivCover);
            } else {
                holder.llMoreImageContainer.setVisibility(View.VISIBLE);
                holder.rivCover.setVisibility(View.GONE);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivCoverOne);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivCoverTwo);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(2), holder.rivCoverThree);
            }
        } else {
            holder.llImageContainer.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getTopicTitle())) {
            holder.tvFromSuject.setText(mContext.getString(R.string.question, data.getTopicTitle()));
        } else {
            holder.tvFromSuject.setText(mContext.getString(R.string.question, ""));
        }
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvTitle.setText(data.getTitle());
        }
        holder.tvFollowNumber.setText(mContext.getString(R.string.selectFollowNumber, String.valueOf(data.getConcernCount())));
        holder.tvAnswerNumber.setText(mContext.getString(R.string.selectAnswerNumber, String.valueOf(data.getAnswerCount())));
        holder.tvFromSuject.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Topic subject = new Topic();
                subject.setTopicName(data.getTopicTitle());

                String topicId = data.getTopicIds();
                if (topicId.contains(",")) {
                    subject.setId(topicId.split(",")[0]);
                } else {
                    subject.setId(topicId);
                }
                TopicDetailActivity.start(mContext, subject, false);
            }
        });
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            User user = (User) v.getTag();
            if (null != user)
                SomeoneHomepageActivity.start(mContext, user, false);
        }
    };

    /**
     * 更新话题UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateSubjectViewHolder(SubjectViewHolder holder, int position, SelectInfo data) {

        SelectSubjcetAdapter adapter = new SelectSubjcetAdapter(mContext, data.getSubjectList());
        holder.wrvSubject.setAdapter(adapter);
        holder.tvMoreSubject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicSquareActivity.start(mContext);
            }
        });
    }

    /**
     * 文章
     */
    class ArticleViewHolder extends BaseRecyclerViewHolder {

        TextView tvFromSuject;
        RoundedImageView rivAvatar;
        TextView tvUserName;
        TextView tvTitle;
        TextView tvContent;
        TextView tvPriseNumber;
        TextView tvCommentNumber;
        RatioImageView rivCover;

        public ArticleViewHolder(View itemView) {
            super(itemView);
            tvFromSuject = (TextView) itemView.findViewById(R.id.tvFromSuject);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvContent = (TextView) itemView.findViewById(R.id.tvContent);
            tvPriseNumber = (TextView) itemView.findViewById(R.id.tvPriseNumber);
            tvCommentNumber = (TextView) itemView.findViewById(R.id.tvCommentNumber);
            rivCover = (RatioImageView) itemView.findViewById(R.id.rivCover);
        }

    }

    /**
     * 回答
     */
    class AnswerViewHolder extends BaseRecyclerViewHolder {

        TextView tvFromSuject;
        RoundedImageView rivAvatar;
        TextView tvUserName;
        TextView tvTitle;
        TextView tvContent;
        TextView tvPriseNumber;
        TextView tvCommentNumber;
        LinearLayout llImageContainer;
        RatioImageView rivCover;
        LinearLayout llMoreImageContainer;
        RatioImageView rivCoverOne;
        RatioImageView rivCoverTwo;
        RatioImageView rivCoverThree;

        public AnswerViewHolder(View itemView) {
            super(itemView);
            tvFromSuject = (TextView) itemView.findViewById(R.id.tvFromSuject);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvContent = (TextView) itemView.findViewById(R.id.tvContent);
            tvPriseNumber = (TextView) itemView.findViewById(R.id.tvPriseNumber);
            tvCommentNumber = (TextView) itemView.findViewById(R.id.tvCommentNumber);
            llImageContainer = (LinearLayout) itemView.findViewById(R.id.llImageContainer);
            rivCover = (RatioImageView) itemView.findViewById(R.id.rivCover);
            llMoreImageContainer = (LinearLayout) itemView.findViewById(R.id.llMoreImageContainer);
            rivCoverOne = (RatioImageView) itemView.findViewById(R.id.rivCoverOne);
            rivCoverTwo = (RatioImageView) itemView.findViewById(R.id.rivCoverTwo);
            rivCoverThree = (RatioImageView) itemView.findViewById(R.id.rivCoverThree);
        }

    }

    /**
     * 问题
     */
    class ProblemViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvUserName;
        TextView tvFromSuject;
        TextView tvTitle;
        TextView tvFollowNumber;
        TextView tvAnswerNumber;
        LinearLayout llImageContainer;
        RatioImageView rivCover;
        LinearLayout llMoreImageContainer;
        RatioImageView rivCoverOne;
        RatioImageView rivCoverTwo;
        RatioImageView rivCoverThree;

        public ProblemViewHolder(View itemView) {
            super(itemView);
            tvFromSuject = (TextView) itemView.findViewById(R.id.tvFromSuject);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvFollowNumber = (TextView) itemView.findViewById(R.id.tvFollowNumber);
            tvAnswerNumber = (TextView) itemView.findViewById(R.id.tvAnswerNumber);
            llImageContainer = (LinearLayout) itemView.findViewById(R.id.llImageContainer);
            rivCover = (RatioImageView) itemView.findViewById(R.id.rivCover);
            llMoreImageContainer = (LinearLayout) itemView.findViewById(R.id.llMoreImageContainer);
            rivCoverOne = (RatioImageView) itemView.findViewById(R.id.rivCoverOne);
            rivCoverTwo = (RatioImageView) itemView.findViewById(R.id.rivCoverTwo);
            rivCoverThree = (RatioImageView) itemView.findViewById(R.id.rivCoverThree);
        }

    }


    /**
     * 话题
     */
    class SubjectViewHolder extends BaseRecyclerViewHolder {

        WrapRecyclerView wrvSubject;
        TextView tvMoreSubject;

        public SubjectViewHolder(View itemView) {
            super(itemView);
            wrvSubject = (WrapRecyclerView) itemView.findViewById(R.id.wrvSubject);
            tvMoreSubject = (TextView) itemView.findViewById(R.id.tvMoreSubject);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            View headerView = new View(mContext);
            headerView.setLayoutParams(new RecyclerView.LayoutParams((int) RT.getDensity() * 9, RecyclerView.LayoutParams.MATCH_PARENT));
            headerView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
            wrvSubject.addHeaderView(headerView);
            wrvSubject.addFootView(new View(mContext));
            wrvSubject.addItemDecoration(new VerticalDividerItemDecoration.Builder(mContext).color(mContext.getResources().getColor(R.color.transparent)).size((int) mContext.getResources().getDimension(R.dimen.splitLineWidth)).build());
            wrvSubject.setLayoutManager(layoutManager);
        }

    }

}
